package com.ytxg.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ytxg.api.dto.UserCommentsDtoR;
import com.ytxg.api.feign.BookInfoClient;
import com.ytxg.api.po.book.BookComment;
import com.ytxg.api.po.book.BookInfo;
import com.ytxg.api.po.user.UserInfo;
import com.ytxg.core.consts.SystemConfigConsts;
import com.ytxg.core.dto.RestResp;
import com.ytxg.core.utils.JwtUtils;
import com.ytxg.core.utils.UserHolder;
import com.ytxg.user.dto.*;

import com.ytxg.user.mapper.UserInfoMapper;
import com.ytxg.user.po.UserComment;
import com.ytxg.user.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.*;


@Service
@RequiredArgsConstructor
public class UserInfoServiceImpl implements UserInfoService {

    private final UserInfoMapper userInfoMapper;

    private final BookInfoClient bookInfoClient;

    /**
     * 用户登录
     * @param userDtoIn
     * @return
     */
    @Override
    public RestResp<UserLoginRespDto> login(UserDtoIn userDtoIn) {
        LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserInfo::getUsername,userDtoIn.getUsername());
        UserInfo userInfo = userInfoMapper.selectOne(lambdaQueryWrapper);
        if(userInfo==null){
            return RestResp.ok(null);
        }

        String s = DigestUtils.md5DigestAsHex(userDtoIn.getPassword().getBytes(StandardCharsets.UTF_8));
        if(!userInfo.getPassword().equals(DigestUtils.md5DigestAsHex(userDtoIn.getPassword().getBytes()))){
            return RestResp.ok(null);
        }


        UserLoginRespDto login = UserLoginRespDto.builder()
                .nickName(userInfo.getNickName())
                .token(JwtUtils.generateToken(userInfo.getId(), SystemConfigConsts.NOVEL_FRONT_KEY))
                .uid(userInfo.getId())
                .build();
        return RestResp.ok(login);

    }

    /**
     * 获取用户信息
     * @return
     */
    @Override
    public RestResp<UserInfoRespDto> getUserInfo(Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        UserInfoRespDto build = UserInfoRespDto.builder().build();
        BeanUtils.copyProperties(userInfo,build);
        return RestResp.ok(build);
    }

    /**
     * 获取用户评论
     * @param userCommentsDtoR
     * @return
     */
    @Override
    public RestResp getComments(UserCommentsDtoR userCommentsDtoR) {
        List<UserCommentRespDto> ret = new ArrayList<>();
        Long userId = UserHolder.getUserId();
        //评论s
        List<BookComment> bookComment = bookInfoClient.getBookComment(userId, userCommentsDtoR.getPageNum(), userCommentsDtoR.getPageSize());


        for (BookComment comment : bookComment) {
            Long bookId = comment.getBookId();
            UserCommentRespDto build = UserCommentRespDto.builder()
                    .commentContent(comment.getCommentContent())
                    .commentBook(bookInfoClient.getBookInfoById(bookId).getBookName())
                    .commentTime(comment.getCreateTime())
                    .commentBookPic(bookInfoClient.getBookInfoById(bookId).getPicUrl())
                    .build();
            ret.add(build);
        }
        PageRespDto build = PageRespDto.builder()
                .pageNum(userCommentsDtoR.getPageNum())
                .list(ret)
                .pageSize(userCommentsDtoR.getPageSize())
                .total(ret.size())
                .build();

        return RestResp.ok(build);
    }

}
